//
//  ShelterView.m
//  mcrmSAAS
//
//  Created by yuxiuyi on 15/10/13.
//  Copyright © 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "ShelterView.h"
#import "XUIPanelWindow.h"

@interface ShelterView ()<XUIButtonDelegate,UIGestureRecognizerDelegate>
{
    XUIPanelWindow *_window;
    
    NSString *_title;
    
    NSString *_cancelButtonImageName;
    NSString *_otherButtonImageName;
    
    NSTextAlignment _textAlignment;
    
    XUIButton *_cancelBtn;
    XUIButton *_otherBtn;
}

@property (nonatomic,assign) id<ShelterViewDelegate> delegate;
@end

@implementation ShelterView

- (id)initWithContainViewWithdelegate:(id<ShelterViewDelegate>)delegate cancelButtonImageName:(NSString *)cancelButtonImageName otherButtonImageName:(NSString *)otherButtonImageName completion:(void (^)(UIView *))completion
{
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height)];
    
    _delegate = delegate;
    
    _cancelButtonImageName = cancelButtonImageName;
    
    _otherButtonImageName = otherButtonImageName;
    
    if([UIDevice isIOS8OrNewer] == YES)
    {
        UIView *snapshotView = [[UIApplication sharedApplication].keyWindow snapshotViewAfterScreenUpdates:NO];
        
        //在背景图上面加入了模糊化效果
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectview.frame = self.bounds;
        effectview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        [snapshotView addSubview:effectview];
        
        [self addSubview:snapshotView];
    }
    else
    {
        self.backgroundColor = [UIColor colorWithHex:0X000000 alpha:0.8];
    }
    
    CGFloat y = 90,y1 = 97,x1 = 43;;

    if ([UIDevice is35InchScreen])
    {
        y1 = 40;
        x1 = 30;
        y = 50;
    }
    else if ([UIDevice is40InchScreen])
    {
        x1 = 30;
        y = 70;
    }
    
    UIView *containview = [[UIView alloc]initWithFrame:CGRectMake(x1, y1 *kHeightScale, self.width - x1*2, 343)];
    containview.backgroundColor = [UIColor whiteColor];
    containview.layer.cornerRadius = 3;
    containview.layer.masksToBounds = YES;
    [self addSubview:containview];
    
    if(_cancelButtonImageName.length >0)
    {
        UIImage *btnImage = [UIImage imageNamed:_cancelButtonImageName];
        _cancelBtn = [[XUIButton alloc]initWithFrame:CGRectMake(10, 10, btnImage.size.width, btnImage.size.height)];
        [_cancelBtn SetBackgroundImage:btnImage];
        _cancelBtn.centerX = self.centerX;
        _cancelBtn.bottom = self.bottom - y*kHeightScale;
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_cancelBtn];
    }
    
    if(_otherButtonImageName.length > 0)
    {
        UIImage *btnImage = [UIImage imageNamed:_otherButtonImageName];
        _otherBtn = [[XUIButton alloc]initWithFrame:CGRectMake(10, 10, btnImage.size.width, btnImage.size.height)];
        [_otherBtn SetBackgroundImage:btnImage];
        _otherBtn.centerX = self.centerX;
        _otherBtn.bottom = self.bottom - y*kHeightScale;
        [_otherBtn addTarget:self action:@selector(otherBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_otherBtn];
        
        if (_cancelButtonImageName.length >0) {
            
            CGFloat borderLeftRightMargin = (self.width - btnImage.size.width*2)/4;
            _cancelBtn.left = self.left + borderLeftRightMargin;
            _otherBtn.right = self.right - borderLeftRightMargin;
        }
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    completion(containview);
    
    return self;
}

- (id)initWithContainView:(UIView *)containView delegate:(id<ShelterViewDelegate>)delegate cancelButtonImageName:(NSString *)cancelButtonImageName otherButtonImageName:(NSString*)otherButtonImageName
{
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height)];

    _delegate = delegate;
    
    _cancelButtonImageName = cancelButtonImageName;
    
    _otherButtonImageName = otherButtonImageName;
    
    if([UIDevice isIOS8OrNewer] == YES)
    {
        UIView *snapshotView = [[UIApplication sharedApplication].keyWindow snapshotViewAfterScreenUpdates:NO];
        
        //在背景图上面加入了模糊化效果
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectview.frame = self.bounds;
        effectview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        [snapshotView addSubview:effectview];
        
        [self addSubview:snapshotView];
    }
    else
    {
        self.backgroundColor = [UIColor colorWithHex:0X000000 alpha:0.8];
    }
    
    [self addSubview:containView];
    
    CGFloat y = 90;
    if([UIDevice is35InchScreen]||[UIDevice is40InchScreen])
    {
        y = 70;
    }
    
    if(_cancelButtonImageName.length >0)
    {
        UIImage *btnImage = [UIImage imageNamed:_cancelButtonImageName];
        _cancelBtn = [[XUIButton alloc]initWithFrame:CGRectMake(10, 10, btnImage.size.width, btnImage.size.height)];
        [_cancelBtn SetBackgroundImage:btnImage];
        _cancelBtn.centerX = self.centerX;
        _cancelBtn.bottom = self.bottom - y*kHeightScale;
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_cancelBtn];
    }
    
    if(_otherButtonImageName.length > 0)
    {
        UIImage *btnImage = [UIImage imageNamed:_otherButtonImageName];
        _otherBtn = [[XUIButton alloc]initWithFrame:CGRectMake(10, 10, btnImage.size.width, btnImage.size.height)];
        [_otherBtn SetBackgroundImage:btnImage];
        _otherBtn.centerX = self.centerX;
        _otherBtn.bottom = self.bottom - y*kHeightScale;
        [_otherBtn addTarget:self action:@selector(otherBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_otherBtn];
        
        if (_cancelButtonImageName.length >0) {
            
            CGFloat borderLeftRightMargin = (self.width - btnImage.size.width*2)/4;
            _cancelBtn.left = self.left + borderLeftRightMargin;
            _otherBtn.right = self.right - borderLeftRightMargin;
        }
    }

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    return self;
}

- (void)show
{
    _window = [XUIPanelWindow new];
    _window.responder = self;
    
    self.superview.userInteractionEnabled = NO;
    
    self.alpha = 0;
    
    [UIView animateWithDuration:0.3 animations:^
    {
        self.alpha = 1;
    } completion:^(BOOL finished)
    {
        self.superview.userInteractionEnabled = YES;
    }];
}

-(void)otherBtnClick:(XUIButton*)sender
{
    if([_delegate respondsToSelector:@selector(clickedOtherButtonOfshelterView:)])
        [_delegate clickedOtherButtonOfshelterView:self];
    sender.selected = YES;
    [self close];
}

-(void)cancelBtnClick:(XUIButton*)sender
{
    if([_delegate respondsToSelector:@selector(clickedCancelButtonOfShelterView:)])
        [_delegate clickedCancelButtonOfShelterView:self];
    [self close];
}

-(void)close
{
    self.superview.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.3 animations:^
    {
         self.alpha = 0;
    } completion:^(BOOL finished)
    {
        self.superview.userInteractionEnabled = YES;
        
        [self removeFromSuperview];
        
        ReleaseObj(_window);
    }];
}

- (void)tapGestureRecognizer:(UITapGestureRecognizer*)tapGesture
{
    if([_delegate respondsToSelector:@selector(clickedCancelButtonOfShelterView:)])
        [_delegate clickedCancelButtonOfShelterView:self];
    [self close];
}
@end
